package com.itheima.project.face.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.project.constant.sms.SmsCacheConstant;
import com.itheima.project.core.enums.sms.SmsChannelEnum;
import com.itheima.project.exception.ProjectException;
import com.itheima.project.face.SmsChannelFace;
import com.itheima.project.pojo.SmsChannel;
import com.itheima.project.service.ISmsChannelService;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.utils.EmptyUtil;
import com.itheima.project.utils.ExceptionsUtil;
import com.itheima.project.vo.basic.OtherConfigVo;
import com.itheima.project.vo.sms.SmsChannelVo;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName SmsChannelFaceImpl.java
 * @Description 渠道Face接口实现
 */
@Slf4j
@Component
public class SmsChannelFaceImpl implements SmsChannelFace {

    @Autowired
    ISmsChannelService smsChannelService;


    @Override
    @Cacheable(value = SmsCacheConstant.PAGE_CHANNEL,key ="#pageNum+'-'+#pageSize+'-'+#smsChannelVo.hashCode()")
    public Page<SmsChannelVo> findSmsChannelVoPage(SmsChannelVo smsChannelVo,
                                                   int pageNum,
                                                   int pageSize) throws ProjectException {
        try {
            Page<SmsChannel> page = smsChannelService.findSmsChannelVoPage(smsChannelVo, pageNum, pageSize);
            Page<SmsChannelVo> pageVo = BeanConv.toPage(page,SmsChannelVo.class);
            //结果集转换
            List<SmsChannelVo> smsChannelVos =pageVo.getRecords();
            if (!EmptyUtil.isNullOrEmpty(smsChannelVos)){
                smsChannelVos.forEach(n->{
                    List <OtherConfigVo> list = JSONArray.parseArray(n.getOtherConfig(),OtherConfigVo.class);
                    n.setOtherConfigs(list);
                });
            }
            return pageVo;
        } catch (Exception e) {
            log.error("查询通道列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsChannelEnum.PAGE_FAIL);
        }
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = SmsCacheConstant.PAGE_CHANNEL,allEntries = true),
            @CacheEvict(value = SmsCacheConstant.CHANNEL_LIST,allEntries = true),
            @CacheEvict(value = SmsCacheConstant.CHANNEL_LABEL,key = "#smsChannelVo.channelLabel")},
            put={@CachePut(value =SmsCacheConstant.PREFIX_CHANNEL,key = "#result.channelLabel")})
    public SmsChannelVo createSmsChannel(SmsChannelVo smsChannelVo) throws ProjectException{
        try {
            return BeanConv.toBean( smsChannelService.createSmsChannel(smsChannelVo), SmsChannelVo.class);
        } catch (Exception e) {
            log.error("保存通道异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsChannelEnum.CREATE_FAIL);
        }
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = SmsCacheConstant.PAGE_CHANNEL,allEntries = true),
            @CacheEvict(value = SmsCacheConstant.CHANNEL_LIST,allEntries = true),
            @CacheEvict(value = SmsCacheConstant.CHANNEL_LABEL,key = "#smsChannelVo.channelLabel"),
            @CacheEvict(value = SmsCacheConstant.PREFIX_CHANNEL,key = "#smsChannelVo.channelLabel")})
    public SmsChannelVo updateSmsChannel(SmsChannelVo smsChannelVo) throws ProjectException{
        try {
            SmsChannel smsChannel = smsChannelService.updateSmsChannel(smsChannelVo);
            return BeanConv.toBean(smsChannel,smsChannelVo.getClass());
        } catch (Exception e) {
            log.error("保存通道异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsChannelEnum.UPDATE_FAIL);
        }
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = SmsCacheConstant.PAGE_CHANNEL,allEntries = true),
            @CacheEvict(value = SmsCacheConstant.CHANNEL_LIST,allEntries = true),
            @CacheEvict(value = SmsCacheConstant.CHANNEL_LABEL,allEntries = true),
            @CacheEvict(value = SmsCacheConstant.PREFIX_CHANNEL,allEntries = true)})
    public Boolean deleteSmsChannel(String[] checkedIds) throws ProjectException{
        try {
            return smsChannelService.deleteSmsChannel(checkedIds);
        } catch (Exception e) {
            log.error("删除通道异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsChannelEnum.DELETE_FAIL);
        }
    }

    @Override
    @Cacheable(value =SmsCacheConstant.CHANNEL_LABEL,key = "#channelLabel")
    public SmsChannelVo findSmsChannelByChannelLabel(String channelLabel)throws ProjectException {
        try {
            SmsChannel smsChannel = smsChannelService.findSmsChannelByChannelLabel(channelLabel);
            return BeanConv.toBean(smsChannel,SmsChannelVo.class);
        } catch (Exception e) {
            log.error("查找通道异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsChannelEnum.SELECT_FAIL);
        }
    }

    @Override
    @Cacheable(value =SmsCacheConstant.CHANNEL_LIST)
    public List<SmsChannelVo> findSmsChannelVoList() throws ProjectException{
        try {
            return BeanConv.toBeanList(smsChannelService.findSmsChannelVoList(),SmsChannelVo.class);
        } catch (Exception e) {
            log.error("查找所有通道异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsChannelEnum.SELECT_FAIL);
        }
    }
}
